package com.example.juc.until;

import org.junit.Test;

import java.util.AbstractQueue;
import java.util.Deque;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@SuppressWarnings("all")
public class BlockingQueueTest {
    //非阻塞队列的四种api   可以嵌套使用
    //队列空间大小为3
    private BlockingQueue blockingQueue =new ArrayBlockingQueue(3);

@Test
    public void Test1(){
    //抛异常
    System.out.println(blockingQueue.add("a"));
    System.out.println(blockingQueue.add("b"));
    System.out.println(blockingQueue.add("c"));
    //添加四个元素抛异常  java.lang.IllegalStateException: Queue full
    //System.out.println(blockingQueue.add("c"));

    //检测对首元素  如果没有元素抛异常
    System.out.println(blockingQueue.element());

    System.out.println(blockingQueue.remove());
    System.out.println(blockingQueue.remove());
    System.out.println(blockingQueue.remove());
    //移除第四个时因为没有这个元素所以抛异常  java.util.NoSuchElementException
   // System.out.println(blockingQueue.remove());


}
@Test
    public void Test2(){
       //有返回值 不抛异常
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        //添加四个元素不会抛异常 返回null
        System.out.println(blockingQueue.offer("c"));

       System.out.println("+++++++++++++++++++++++++++");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        //移除第四个时抛异常  返回null
         System.out.println(blockingQueue.poll());
         System.out.println("+++++++++++++++++++++++++++");

        //检测对首元素 不会抛异常 返回null
       System.out.println(blockingQueue.peek());

}

    @Test
    public void Test3() throws InterruptedException {
        //阻塞等待
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        //因为队列大小是3  添加第四条元素时  没有空间 当有其他元素移除后  会添加=第四条元素  取消阻塞等待
        blockingQueue.put("c");

        System.out.println("+++++++++++++++++++++++++++");
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        //因为队列大小是3  移除第四条元素时  没有空间 当有其他元素添加后  会移除=第四条元素  取消阻塞等待
        System.out.println(blockingQueue.take());
        System.out.println("+++++++++++++++++++++++++++");

    }

    @Test
    public void Test4() throws InterruptedException {
        //超时等待
        System.out.println(blockingQueue.offer("a",2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("b",2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("c",2, TimeUnit.SECONDS));
        //添加四个元素不会抛异常 返回null,
        System.out.println(blockingQueue.offer("c",2, TimeUnit.SECONDS));

        System.out.println("+++++++++++++++++++++++++++");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        //移除第四个时抛异常  返回null
        System.out.println(blockingQueue.poll());
        System.out.println("+++++++++++++++++++++++++++");

        //检测对首元素 不会抛异常 返回null
        System.out.println(blockingQueue.peek());

    }
}
